package com.jolink.omiis.wechat.schedule;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.jolink.omiis.wechat.WeChatContext;
import com.jolink.omiis.wechat.token.IAuthorizerAccessTokenService;

@Service
public class RefreshTokenJob {

	private static Logger logger = LoggerFactory.getLogger(RefreshTokenJob.class);
	@Autowired
	private WeChatContext wechatContext;

	@Scheduled(cron = "0 0/25 * * * ?")
	public void doJob() {
		long t1 = System.currentTimeMillis();
		boolean isLocal = "true"
				.equals(wechatContext.getServiceUtil().getConfiguration().getValue("OMIIS_PLATFORM_LOCAL"));
		logger.info("开始执行AuthorizerAccessToken刷新任务，本地服务标识：{}。", isLocal);
		if (isLocal) {
			return;
		}
		try {
			IAuthorizerAccessTokenService aats = wechatContext.getServiceUtil()
					.getService(IAuthorizerAccessTokenService.class);
			aats.refresh();
		} catch (Exception e) {
			logger.error("刷新AuthorizerAccessToken发生异常：" + e.getMessage(), e);
		}
		long t2 = System.currentTimeMillis();
		logger.info("执行AuthorizerAccessToken刷新任务完成，耗时：{}毫秒。", (t2 - t1));
	}
}
